package com.rn.gypsophila.redis.web;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rn.gypsophila.redis.queue.blocking.RedisMessageConsumer;
import com.rn.gypsophila.redis.queue.blocking.RedisMessageConsumer2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.*;

/**
 * Description：Web Runner
 *
 * @author rannuo
 * @date 2020/5/12
 */
@Slf4j
@Component
public class WebRunner implements CommandLineRunner {

    private ExecutorService pool;

    @Autowired
    private RedisMessageConsumer redisMessageConsumer;
    @Autowired
    private RedisMessageConsumer2 redisMessageConsumer2;

    @PostConstruct
    public void init() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
        // Common Thread Pool
        pool = new ThreadPoolExecutor(4, 10,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024),
                namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    @Override
    public void run(String... args) throws Exception {
        pool.submit(redisMessageConsumer);
        pool.submit(redisMessageConsumer2);
    }

}
